package com.unnous.common.utils;

import java.util.Calendar;
import java.util.Date;


/**
 * 日期工具类
 * author unnous
 * 2018-09-25
 */
public class DateUtil {

    /**
     * 获取当前时间 2018-09-10 00:00:00 这样的Date
     * @return 当前时间
     */
    public Date getNow() {
        Calendar now = Calendar.getInstance();
        now.setTime(new Date());
        now.set(Calendar.HOUR_OF_DAY, 0);
        now.set(Calendar.MINUTE, 0);
        now.set(Calendar.SECOND, 0);
        now.set(Calendar.MILLISECOND, 0);
        return now.getTime();
    }

    /**
     * 将日期的时分秒置0
     * @param date 日期
     * @return 日期
     */
    public static Date getYMD(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }
    /**
     * :获取该日期所在周的周一
     * @param date 日期
     * @return 返回星期一的时间
     */
    public static Date getMondayOfThisWeek(Date date) {
        if(date == null){
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0) day_of_week = 7;
        c.add(Calendar.DATE, -day_of_week + 1);
        return c.getTime();
    }

    /**
     * 获取该日期所在周的周日
     * @param date 日期
     * @return 返回星期日的时间
     */
    public static Date getSundayOfThisWeek(Date date) {
        if(date == null){
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int day_of_week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0) day_of_week = 7;
        calendar.add(Calendar.DATE, -day_of_week + 7);
        return calendar.getTime();
    }

    /**
     * 获取某一年中的第几周的周几 的日期
     * @param year 年份
     * @param weeks 一年中的第几周
     * @param dayOfWeek  1表示周日，2表示周一，7表示周六
     * @return 返回根据某年中的周数 获取星期几的日期
     */
    public static Date getWeekByWeeksOfYear(int year, int weeks, int dayOfWeek, int firstDayOfWeek) {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(firstDayOfWeek);
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.WEEK_OF_YEAR, weeks);
        calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取该日期是所在年是第几周
     * @param date 日期
     * @return 返回日期所在年的周数
     */
    public static int getWeeksOfYearByDate(Date date) {
        if(date == null){
            return 0;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * 获取该日期所在月的第一天
     * @param date 日期
     * @return 月的第一天
     */
    public static Date getFirstDayOfMonth(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getYMD(date));
        calendar.set(Calendar.DATE, 1);
        return calendar.getTime();
    }

    /**
     * 获取该日期所在月的最后一天
     * @param date 日期
     * @return 月的最后一天
     */
    public static Date getLastDayOfMonth(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getYMD(date));
        calendar.set(Calendar.DATE, 1);
        calendar.roll(Calendar.DATE, -1);
        return calendar.getTime();
    }
}
